package com.example.purchase.view.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.example.purchase.MyApplication;
import com.example.purchase.R;
import com.example.purchase.adapter.RecyclerViewAdapter;
import com.example.purchase.bean.BaseBean;
import com.example.purchase.bean.MainBean;
import com.example.purchase.databinding.ActivityTicketManageBinding;
import com.example.purchase.network.RetrofitClient;
import com.example.purchase.network.service.HttpService;
import com.example.purchase.view.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Predicate;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class TicketManageActivity extends BaseActivity<ActivityTicketManageBinding> {


    private final HttpService service = RetrofitClient.getInstance().getService(HttpService.class);

    @Override
    protected boolean isStatusBarEnabled() {
        return false ;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_ticket_manage;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEmpty() {

    }

    @Override
    protected void initClick() {
        dataBinding.swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                            initTimeOut();
                            dataBinding.swipeRefresh.setRefreshing(false);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        dataBinding.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TicketManageActivity.this,PublishActivity.class));
            }
        });
    }

    @Override
    protected void initTimeOut() {
        service.getAdminTicket("1",token).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).filter(new Predicate<BaseBean<List<MainBean>>>() {
            @Override
            public boolean test(BaseBean<List<MainBean>> listBaseBean) throws Throwable {
                return String.valueOf(listBaseBean.getCode()).equals("200");
            }
        }).subscribe(new Consumer<BaseBean<List<MainBean>>>() {
            @Override
            public void accept(BaseBean<List<MainBean>> listBaseBean) throws Throwable {
                if (listBaseBean.getData().size() == 0){
                    dataBinding.recyclerView.setVisibility(View.GONE);
                    dataBinding.empty.setVisibility(View.VISIBLE);
                }else {
                    dataBinding.recyclerView.setVisibility(View.VISIBLE);
                    dataBinding.empty.setVisibility(View.GONE);
                    dataBinding.recyclerView.setAdapter(new RecyclerViewAdapter<List<MainBean>>(listBaseBean.getData(),TicketManageActivity.this, MyApplication.ADMIN_LIST,listBaseBean.getData().size()));
                    dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                }
            }
        });
    }
}